package com.orhon.smartcampus.modules.student.fetcher;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.modules.base.entity.Grades;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLGrades;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.GradeMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.student.dao.EclassDao;
import com.orhon.smartcampus.modules.student.entity.Arrives;
import com.orhon.smartcampus.modules.student.entity.Eclass;
import com.orhon.smartcampus.modules.student.gqlModel.GQLArrives;
import com.orhon.smartcampus.modules.student.gqlModel.GQLEclass;
import com.orhon.smartcampus.modules.student.mm.ArrivesMapper;
import com.orhon.smartcampus.modules.student.mm.EclassMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.utils.R;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class EclassFetcher implements DataFetcher {
    
	@Autowired
    EclassDao eclassDao;

    @Autowired
    TokenService tokenService;


    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {
        String token = tokenService.getSystemToken();
        if(token==null) {
            return  R.NoLogin();
        }
        Long schoolId = JwtUtils.getSchoolId(token);

        int id = Integer.parseInt(env.getArgument("id").toString());

        String hql = "from Eclass where deleted_at = null and id = "+id+" and school_id = " + schoolId;
        Eclass item =  eclassDao.findId(hql, null);
        if(item!=null) {
        	GQLGrades gqlGrades = null;
            if (env.getSelectionSet().contains("grade")){
                Grades grades = item.getBaseGradesByGradeId();
                 gqlGrades = GradeMapper.MAPPER.toGrade(grades);
            }

            GQLArrives gqlArrives = null;
            if (env.getSelectionSet().contains("arrives")){
                Arrives arrives = item.getStudentArrivesByArrivesId();
                gqlArrives = ArrivesMapper.MAPPER.toArrives(arrives);
            }
            
            GQLSchool gqlSchool = null;
            if (env.getSelectionSet().contains("school")){
                Schools schools = item.getBaseSchoolsBySchoolId();
                gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
            }
            
//            GQLBuilding gqlBuilding = null;
//            if (env.getSelectionSet().contains("building")){
//               Buildings buildings = item.getMaterialBuildingsByBuildingsId();
//               gqlBuilding = BuildingMapper.MAPPER.toBuilding(buildings);
//            }
//            
//            GQLUnit gqlUnit = null;
//            if (env.getSelectionSet().contains("unit")){
//               Unit unit = item.getMaterialUnitByUnitId();
//               gqlUnit = UnitMapper.MAPPER.toUnit(unit);
//            }
//            
//            GQLFloor gqlFloor = null;
//            if (env.getSelectionSet().contains("floor")){
//               Floor floor = item.getMaterialFloorByFloorId();
//               gqlFloor = FloorMapper.MAPPER.toFloor(floor);
//            }
//            
//            GQLRoom gqlRoom = null;
//			if (env.getSelectionSet().contains("classroom")){
//				Room room = item.getRoom();
//				gqlRoom = RoomMapper.MAPPER.toRoom(room);
//			}
//            
//            GQLRoom gqlRoom = null;
//            if (env.getSelectionSet().contains("classroom")){
//               Floor floor = item.getMaterialFloorByFloorId();
//               gqlFloor = FloorMapper.MAPPER.toFloor(floor);
//            }
            
//            GQLSite gqlSite = null;
//            if (env.getSelectionSet().contains("site")){
//               Floor floor = item.get
//               gqlFloor = FloorMapper.MAPPER.toFloor(floor);
//            }
            GQLEclass eclass = EclassMapper.MAPPER.toEclass(item);
            eclass.setGrade(gqlGrades);
            eclass.setArrives(gqlArrives);
            eclass.setSchool(gqlSchool);
//            eclass.setBuilding(gqlBuilding);
//            eclass.setUnit(gqlUnit);
//            eclass.setFloor(gqlFloor);
//            eclass.setClassroom(gqlRoom);
//           
            return eclass;
        }
		return null;
        
    }


}
